package com.hdyg.sixg.fragment;

import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.trans.PendOrderActivity;
import com.hdyg.sixg.adapter.MenuAdapter;
import com.hdyg.sixg.adapter.base.MyFragmentAdapter;
import com.hdyg.sixg.entry.NativeGuideBean;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.ui.NoPreloadViewPager;
import com.hdyg.sixg.util.DataCenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author CZB
 * @describe 交易
 * @time 2019/4/3 16:55
 */
public class TransFragment extends BaseFragment {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.ll_top_right)
    LinearLayout llTopRight;
    @BindView(R.id.rv_order)
    RecyclerView rvOrder;
    @BindView(R.id.vp_order)
    NoPreloadViewPager vpOrder;

    private List<NativeGuideBean> menuDatas;
    private MenuAdapter menuAdapter;

    private List<BaseFragment> pageDatas;
    private MyFragmentAdapter pageAdapter;


    @Override
    protected void createPresenter() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_trans;
    }

    @Override
    protected void initView() {
        llTopLeft.setVisibility(View.GONE);
        tvTopTitle.setText(R.string.trans_title);
        tvTopRight.setText(R.string.trans_right);
        rvOrder.setLayoutManager(new GridLayoutManager(mContext,4));
        menuDatas = DataCenter.getTransCenterGuideData();
        initMenuAdapter();
        initPager();
    }

    @Override
    protected void initData() {

    }

    //菜单适配器
    private void initMenuAdapter(){
        menuAdapter = new MenuAdapter(R.layout.item_menu,menuDatas);
        rvOrder.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener((adapter, view, position) -> {
            for (int i=0;i<menuDatas.size();i++){
                if (i == position){
                    menuDatas.get(i).setSelect(true);
                }else {
                    menuDatas.get(i).setSelect(false);
                }
            }
            menuAdapter.replaceData(menuDatas);
            menuAdapter.notifyDataSetChanged();
            vpOrder.setCurrentItem(position);
        });
    }
    //具体viewpager
    private void initPager(){
        if (pageDatas == null){
            pageDatas = new ArrayList<>();
        }else {
            pageDatas.clear();
        }
        for (int i = 0; i < menuDatas.size(); i++) {
            pageDatas.add(new TransChildFragment().newInstance(menuDatas.get(i).getType()));
        }
        pageAdapter = new MyFragmentAdapter(getChildFragmentManager(), pageDatas);
        vpOrder.setAdapter(pageAdapter);
    }

    @OnClick({R.id.ll_top_right})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.ll_top_right:
                startActivity(PendOrderActivity.class);
                break;
        }
    }

}
